package com.zhouyibike.uu.ui.activity.redwallet;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.GetCodeResult;
import com.zhouyibike.uu.entity.RedBagInstroduceResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.activity.web.TotalWebActivity;
import com.zhouyibike.uu.ui.base.BaseActivity;

import java.util.HashMap;
import java.util.Map;

public class ScanrwActivity extends BaseActivity {
    private TextView tv1,tv2,tv3,tv4,rw_tvbt;
    private int num=20;
    private int time = 5;
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    private RelativeLayout barly;
    private TextView gongxitv;
    private boolean iscan=false;
    private Handler timehandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            if (time == 1) {
                rw_tvbt.setEnabled(true);
                barly.setEnabled(true);
                mBtnBack.setEnabled(true);
                iscan=true;
                rw_tvbt.setText("我知道了");
                time = 5;
                timehandler.removeMessages(0);
            } else {
                time--;
                rw_tvbt.setText("我知道了(" + time + "S)");
                rw_tvbt.setEnabled(false);
                timehandler.sendEmptyMessageDelayed(0, 1000);
            }
            return false;
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanrw);
        initView();
        initdata();
        initlistener();
        getMsg();
    }
    private void getMsg() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
        addSubscription(apiStores.RedBagInstroduce(params),
                new ApiCallback<RedBagInstroduceResult>() {

                    @Override
                    public void onSuccess(RedBagInstroduceResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            String[] strings = model.getData().getInstroduce().split("\\*");
                            tv1.setText(strings[0]);
                            tv2.setText(strings[1]);
                            tv3.setText(strings[2]);
                            ImageSpan span = new ImageSpan(ScanrwActivity.this, R.mipmap.hongbaoche);
                            SpannableString spanStr = new SpannableString(tv1.getText().toString());
                            spanStr.setSpan(span,5,6, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                            ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#0099EE"));
//        if (num<10)
//        {
//            spanStr.setSpan(colorSpan,27,28, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//        }else if (num<100)
//        {
//            spanStr.setSpan(colorSpan,27,29, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//        }else{
//            spanStr.setSpan(colorSpan,27,30, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//        }
                            tv1.setText(spanStr);
                        }
                        else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);

                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }

    private void initlistener() {
        tv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getWZMsg();
            }
        });
        rw_tvbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    private void initView() {
        gongxitv= (TextView) findViewById(R.id.gongxitv);
        tv1= (TextView) findViewById(R.id.rw_tv1);
        tv2= (TextView) findViewById(R.id.rw_tv2);
        tv3= (TextView) findViewById(R.id.rw_tv3);
        tv4= (TextView) findViewById(R.id.rw_tv4);
        rw_tvbt= (TextView) findViewById(R.id.rw_tvbt);
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        barly= (RelativeLayout) findViewById(R.id.barly);
        barly.setEnabled(false);
        mBtnBack.setEnabled(false);
    }
    private void initdata() {
        if (getIntent().getBooleanExtra("isgone",false)){
            gongxitv.setVisibility(View.GONE);
            mBtnTitle.setText("红包车玩法");
        }else {
            gongxitv.setVisibility(View.VISIBLE);
            mBtnTitle.setText("红包车");
        }
        SpannableString spanStr2 = new SpannableString(tv4.getText().toString());
        UnderlineSpan underlineSpan = new UnderlineSpan();
        spanStr2.setSpan(underlineSpan, 0, spanStr2.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tv4.setText(spanStr2);
        timehandler.sendEmptyMessageDelayed(0,1000);
    }
    private void getWZMsg(){
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token",""));
        params.put("type", "7");
        showProgressDialog();
        addSubscription(apiStores.GetInformationzcxy(params),
                new ApiCallback<GetCodeResult>() {

                    @Override
                    public void onSuccess(GetCodeResult model) {
                        if(model.getStatus()== Config.SUCCESSCODE){
                            Intent i = new Intent(ScanrwActivity.this, TotalWebActivity.class);
                            i.putExtra("title","翼券服务协议");
                            i.putExtra("url",model.getData());
                            startActivity(i);
//                            finish();
                        }else{
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }

    @Override
    public void onBackPressed() {
        if (iscan)
        {
            super.onBackPressed();
        }else {

        }
//        super.onBackPressed();
    }
}
